﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Marmat.Forms
{
    /// <summary>
    /// 扩展组件的设计模式行为。
    /// </summary>
    public class BindingFieldDesigner : System.ComponentModel.Design.ComponentDesigner
    {
        /// <summary>
        /// 获取与设计器关联的组件所支持的设计时谓词。
        /// </summary>
        public override DesignerVerbCollection Verbs
        {
            get
            {
                if (verbCollection is null)
                {
                    verbCollection = new DesignerVerbCollection();
                    verbCollection.Add(new DesignerVerb("生成绑定控件", (sender, e) => this.CreateComponent(this.Component as IBindingSource)));
                }
                return verbCollection;
            }
        }
        private DesignerVerbCollection verbCollection;


        /// <summary>
        /// 创建组件
        /// </summary>
        /// <param name="bindingSource"></param>
        public void CreateComponent(IBindingSource bindingSource)
        {
            IDesignerHost designer = (IDesignerHost)this.GetService(typeof(IDesignerHost));
            this.CreateComponent(designer, this.Component as IBindingSource);
        }
        /// <summary>
        /// 创建组件
        /// </summary>
        /// <param name="designer"></param>
        /// <param name="bindingSource"></param>
        public void CreateComponent(IDesignerHost designer, IBindingSource bindingSource)
        {
            System.Windows.Forms.Form frm = (System.Windows.Forms.Form)designer.RootComponent;
            if (bindingSource is null)
            {
                MessageBox.Show(nameof(bindingSource) + "参数异常，可能需要重新打开Visual Studio,否则无法打开设计界面。");
                return;
            }
            try
            {
                using (Marmat.Forms.Design.CreatecontrolsForm form = new Design.CreatecontrolsForm(frm, bindingSource))
                {
                    if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        int column = 0;
                        System.Windows.Forms.Control container = (System.Windows.Forms.Control)form.comboBox1.SelectedValue;
                        int columnCount = (int)form.BNumericBox1.Value;
                        int left = 12;
                        int top = 12;
                        foreach (Marmat.Forms.Views.DataGrid.Row row in form.BDataGrid1.Rows)
                        {
                            if (Convert.ToBoolean(row["Check"]) == true && row["Type"] is Type type)
                            {
                                IComponent control = type.Assembly.CreateInstance(type.FullName) as IComponent;
                                frm.Container.Add(control);
                                if (control is IBindingField bindingField)
                                {
                                    bindingField.BindingField.DataSource = bindingSource;
                                    bindingField.BindingField.DataMember = row["ColName"]?.ToString();
                                }
                                if (control is System.Windows.Forms.Control ct)
                                {
                                    ct.Location = new System.Drawing.Point(left, top);
                                    container.Controls.Add(ct);
                                    column++;
                                    left += ct.Width + 12;
                                    if (column == columnCount)
                                    {
                                        column = 0;
                                        left = 12;
                                        top += ct.Height + 6;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "运行异常", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }

        }
    }
}
